
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:flutter_widget_from_html/flutter_widget_from_html.dart';
import 'package:get/get.dart';
import 'package:smart_freezer/core/utils/tr_extension.dart';
import 'package:smart_freezer/generated/locale_keys.g.dart';

import '../resources/app_config.dart';
import '../service/api/app_api.dart';
import '../service/service/result_data.dart';
import '../utils/local_storage.dart';

/// 注册协议
getAgreement(BuildContext context, VoidCallback? confirmCallback) async {
  double contentWidth = MainCommon.screenWidth - 2 * 20;

  ResultData data = await AppApi.getInstance().getRegisterAgree();
  if(data.code == 0){
    if(data.response == null) return;

    /// 注册协议
    var agreeStr = "";
    final language = LocalStorage.getLanguageValue();
    if(language == 1){ // 简中
      agreeStr = data.response!["data"]["userAgreement"];
    }else if(language == 2){ // 繁中
      agreeStr = data.response!["data"]["userAgreementTw"];
    }else if(language == 3){  // 英语
      agreeStr = data.response!["data"]["userAgreementEn"];
    }

    showDialog(
      context: context,
      builder: (context){
        return AlertDialog(
          contentPadding: EdgeInsets.fromLTRB(5, 5, 5, 5),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10),
          ),
          content: Container(
            height: MainCommon.screenHeight * 0.7,
            child: Column(
              children: [
                Expanded(child: SingleChildScrollView(child: HtmlWidget(agreeStr))),

                Container(
                  width: contentWidth * 0.5,
                  margin: EdgeInsets.fromLTRB(0, 5, 0, 0),
                  height: 44,
                  child: TextButton(
                    onPressed: (){
                      Get.back();
                      if(confirmCallback != null){
                        confirmCallback();
                      }
                    },
                    child: Text(LocaleKeys.add_device_confirm.trc(), style: TextStyle(fontSize: 16),),
                  ),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(22),
                      gradient: LinearGradient(colors: MainCommon.jianBianColorList),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );

  }else{
    SmartDialog.showToast(data.msg);
  }
}

